﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal.PartyRepresentative
{
    public partial class PartyRepresentativeInfoDetail : UseMorePageMasterPage
    {
        /// <summary>
        /// 工作室ID
        /// </summary>
        private string StudioID
        {
            get
            {
                return this.Request["StudioID"] ?? "null";
            }
        }
        /// <summary>
        /// 党代表ID
        /// </summary>
        private string RepresentativeID
        {
            get
            {
                return this.Request["id"] ?? "null";
            }
        }
        
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();

            }
        }

        public void LoadData()
        {
            this.hdRepresentativeID.Value = RepresentativeID;
            var representative = DB.PartyRepresentativeInfos.Find(hdRepresentativeID.Value);
            if (representative == null)
            {
                Response.Redirect("~/default.aspx");
            }
            var studio = DB.PartyRepresentativeStudios.Find(representative.StudioID);
            if (studio == null)
            {
                Response.Redirect("~/default.aspx");
            }
            imgUser.ImageUrl = UpFileBusiness.GetFileUrl(representative.ImageID);
            this.litUserName.Text = representative.UserName;
            this.litPost.Text = representative.Post;
            this.litStudio.Text = studio.StudioName;
            this.litOp.Text = string.Format("<a href='MeetingWith.aspx?StudioID={0}&RepresentativeID={1}' class='mybtn' style='dispaly: block;margin-rignt:20px;color: #fff;float: left;'>约见党代表</a>" +
                                            "<a href='Suggestions.aspx?StudioID={0}&RepresentativeID={1}' class='mybtn' style='dispaly: block;color: #fff;float: right;'>心声传递</a>", StudioID, RepresentativeID);
        }

        private void WriteList()
        {
            int startIndex = (Convert.ToInt32(this.PageIndex) - 1) * Convert.ToInt32(this.PageSize);
            DataTable dt = PartyRepresentativeMeeting.GetGridData(startIndex, Convert.ToInt32(PageSize), "4", "", "",RepresentativeID).Tables[0];

            dt.Columns.Add("Title2", typeof(string));
            dt.Columns.Add("BeginTime2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                dr["Title2"] = dr["Title"] + PartyRepresentativeInfo.GetPartyRepresentativeJoin(DB, Convert.ToString(dr["ID"]));
                dr["BeginTime2"] = Convert.ToDateTime(dr["BeginTime"]).ToString("yyyy-MM-dd HH:mm");
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyRepresentativeMeeting.GetGridData(0, 1, "4", "", "", RepresentativeID).Tables[1].Rows[0][0].ToString());
            Response.End();
        }

        #region 模块菜单
        /// <summary>
        /// 初始化module  如果通过url传参可不设置
        /// </summary>
        protected override void InitModule()
        {
            Category = "PartyRepresentativeInfo";
        }
        #endregion
    }
}